// pages/home/mapFind/mapFind.js
const key = 'UBSBZ-QXQL3-5V63Z-YHR2C-D2M5T-EIFOT'; //使用在腾讯位置服务申请的key
const referer = '番石榴叶找房'; //调用插件的app的名称
const chooseLocation = requirePlugin('chooseLocation');
import Https from "../../../utils/api.js"
const http = new Https
Page({

  /**
   * 页面的初始数据
   */
  data: {
    latitude: 0,
    longitude: 0,
    markers: []
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let markers = []
    var marker = {
      id: 0,
      latitude: wx.getStorageSync('latitude'),
      longitude: wx.getStorageSync('longitude'),
      iconPath: "../../../assets/biaojidianwei.png",
      width: 30,
      height: 30,
      label: {
        content: wx.getStorageSync('userInfo').nick_name,
        color: '#FF0202', //文本颜色
        borderRadius: 3, //边框圆角
        borderWidth: 1, //边框宽度
        borderColor: '#FF0202', //边框颜色
        bgColor: '#ffffff', //背景色
        padding: 5, //文本边缘留白
        textAlign: 'center' //文本对齐方式。有效值: left, right, center
      }
    }
    markers.push(marker)
    var latitude = wx.getStorageSync('latitude')
    var longitude = wx.getStorageSync('longitude')
    var province = wx.getStorageSync('province')
    var numbers = 5;
    http.request('secondHouse/house', {
      latitude: latitude,
      longitude: longitude,
      province: province,
      numbers: numbers
    }).then(res => {
      var data = res.data
      var arr = []
      for (const iterator of data) {
        var marker = {
          id: iterator.id,
          latitude: parseFloat(iterator.latitude),
          longitude: parseFloat(iterator.longitude),
          iconPath: "../../../assets/bottom.png",
          width: 30,
          height: 30,
          label: {
            content: iterator.address,
            color: '#FF0202', //文本颜色
            borderRadius: 3, //边框圆角
            borderWidth: 1, //边框宽度
            borderColor: '#FF0202', //边框颜色
            bgColor: '#ffffff', //背景色
            padding: 5, //文本边缘留白
            textAlign: 'center' //文本对齐方式。有效值: left, right, center
          }
        }
        markers.push(marker)
      }
    })
    var that = this
    setTimeout(function () {
      console.log(markers)
      that.setData({
        latitude: wx.getStorageSync('latitude'),
        longitude: wx.getStorageSync('longitude'),
        markers: markers
      })
    }, 2000)
  },

  find() {
    var latitude = wx.getStorageSync('latitude')
    var longitude = wx.getStorageSync('longitude')
    var province = wx.getStorageSync('province')
    var numbers = 5;
    http.request('secondHouse/house', {
      latitude: latitude,
      longitude: longitude,
      province: province,
      numbers: numbers
    }).then(res => {
      console.log(res)
    })
  },

  jump(e) {
    console.log(e)
    var location = JSON.stringify({
      latitude: e.detail.latitude,
      longitude: e.detail.longitude
    });
    wx.navigateTo({
      url: 'plugin://chooseLocation/index?key=' + key + '&referer=' + referer + '&location=' + location
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const location = chooseLocation.getLocation();
    if (location != null) {
      http.request('secondHouse/map/detail', {
        latitude: location.latitude,
        longitude: location.longitude
      }).then(res => {
        console.log(res)
        var info = res.data[0]
        chooseLocation.setLocation(null);
        wx.navigateTo({
          url: '../homeRent/personalHousing/personalHousing?id=' + info.id
        })
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    chooseLocation.setLocation(null);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})